package com.iottepa.support.webMvc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.*;
import org.springframework.web.servlet.config.annotation.*;

import java.nio.charset.Charset;

/**
 * 拦截器配置
 */
@Configuration
public class WebApiConfig implements WebMvcConfigurer {

//    @Autowired
//    private FileUploadTypeInterceptor fileUploadTypeInterceptor;

    /**
     * 解决返回前端数据乱码问题
     * @return
     */
    @Bean
    public HttpMessageConverter responseBodyConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }

    /**
     * 支持跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedHeaders("*")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .maxAge(3600);
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        // 注册自定义拦截器，添加拦截路径和排除拦截路径
//        // 添加文件上传类型拦截器
//        registry.addInterceptor(fileUploadTypeInterceptor).addPathPatterns("/**");
//
//        super.addInterceptors(registry);
//    }
}
